package com.smartguard.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartguard.bean.Device;
import com.smartguard.bean.User;
import com.smartguard.common.Const;
import com.smartguard.service.DeviceService;
import com.smartguard.util.JSONUtil;

public class DeviceAction extends BaseAction {
	private DeviceService deviceService;
	
	private Device device;
	
	public DeviceAction(){
		this.deviceService = new DeviceService();
	}
	
	/**
	 * 往数据库中添加设备信息
	 * @throws IOException
	 */
	public void insertDevice() throws IOException{
		User user= (User) this.getSession().get(Const.SESSION_USER);
		PrintWriter out = this.getWriter();
		if(user == null){
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			out.write(json.toJSONString());
		}
		else{
			this.device.setUsername(user.getUsername());	//注入当前用户的用户名
			this.device.setIsSensor(true);					//硬编码成感应加速度
			//先直接设值==
			this.device.setIsOnline(true);
			this.device.setRemaindPower((double)new Random().nextInt(99)+1);//随机设值
			//检测是否重复添加
			if(this.deviceService.findDeviceByDeviceHash(device.getDeviceHash())==null){
				if(this.deviceService.insertDevice(device)){
					JSONObject json = JSONUtil.newSuccessJSONObject();
					json.put("device", device);
					out.write(json.toJSONString());
				}
				else{
					JSONObject json = JSONUtil.newErrMsgJSONObject("添加失败");
					out.write(json.toJSONString());
				}
			}
			else{
				JSONObject json = JSONUtil.newErrMsgJSONObject("该设备已经添加，请勿重复添加");
				out.write(json.toJSONString());
			}
		}
		out.flush();
		out.close();
	}
	
	/**
	 * 用户获取其拥有的所有设备的列表s
	 * @throws IOException
	 */
	public void getAllDeviceList() throws IOException{
		User user= (User) this.getSession().get(Const.SESSION_USER);
		PrintWriter out = this.getWriter();
		
		if(user == null){
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			out.write(json.toJSONString());
			System.out.println("test get all devicelist" + json.toJSONString());
		}
		else{
			List<Device> deviceList = this.deviceService.findDeviceByUsername(user.getUsername());
			JSONArray jsonArray = JSONUtil.listToJSONArray(deviceList);
			JSONObject json = JSONUtil.newSuccessJSONObject();
			json.put("deviceList", jsonArray);
			out.write(json.toJSONString());
			System.out.println("test get all devicelist" + json.toJSONString());
		}
		out.flush();
		out.close();
	}
	
	/**
	 * 获取当前用户在线设备的列表
	 * @throws IOException
	 */
	public void getOnlineDeviceList() throws IOException{
		PrintWriter out = this.getWriter();
		User user = (User) this.getSession().get(Const.SESSION_USER);
		if(user == null){
			out.write(JSONUtil.newErrMsgJSONObject("未登录").toJSONString());
		}
		else{
			CopyOnWriteArrayList onlineDeviceList = this.getUserOnlineDevice(user.getUsername());
			
			if(onlineDeviceList == null){
				JSONObject json = JSONUtil.newErrMsgJSONObject("您当前没有设备在线");
				out.write(json.toJSONString());
			}
			
			else{
				JSONArray jsonArray = JSONUtil.listToJSONArray(onlineDeviceList);
				out.write(jsonArray.toJSONString());
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 修改设备信息。客户端需要将完整的device信息传过来（包括新信息和旧信息）
	 * @throws IOException
	 */
	public void updateDevice() throws IOException{
		User user = (User) this.getSession().get(Const.SESSION_USER);
		
		PrintWriter out = this.getWriter();
		if(user == null){
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			out.write(json.toJSONString());
		}
		else{
			if(device == null){
				out.write(JSONUtil.newErrMsgJSONObject("设备信息为空").toJSONString());
			}
			else{
				String deviceHash = device.getDeviceHash();	//获取设备的哈希标识值作为索引
				device.setUsername(user.getUsername()); 	//注入当前用户的用户名（至关重要）
				
				//查找此设备有没有注册
				if(this.deviceService.findDeviceByDeviceHash(deviceHash) == null){
					out.write(JSONUtil.newErrMsgJSONObject("此设备暂未注册").toJSONString());
				} else {
					if(this.deviceService.updateDevice(deviceHash, device)){
						out.write(JSONUtil.newSuccessJSONObject().toJSONString());
					}
					else{
						out.write(JSONUtil.newErrMsgJSONObject("修改设备信息失败").toJSONString());
					}
				}
				
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 获取某个设备的信息
	 * @throws IOException 
	 */
	public void getDeviceInfo() throws IOException{
		PrintWriter out = this.getWriter();
		
		if(this.device != null && !"".equals(this.device.getDeviceHash())){
			System.out.println(device.getDeviceHash());
			Device result = this.deviceService.findOneDevice(device);
			if(result != null){
				JSONObject json = JSONUtil.newSuccessJSONObject();
				json.put("device", JSON.parseObject(JSON.toJSON(result).toString()));
				out.write(json.toJSONString());
			} 
			else {
				out.write(JSONUtil.newErrMsgJSONObject("未找到设备").toJSONString());
			}
		}
		else{
			out.write(JSONUtil.newErrMsgJSONObject("缺少设备号参数").toJSONString());
		}
		
		out.flush();
		out.close();
	}

	/**
	 * 登录某个设备的信息
	 * @throws IOException 
	 */
	public void loginDevice() throws IOException{
		PrintWriter out = this.getWriter();
		
		if(this.device != null && !"".equals(this.device.getDeviceHash())){
			System.out.println(device.getDeviceHash());
			Device result = this.deviceService.findOneDevice(device);
			if(result != null){
				JSONObject json = JSONUtil.newSuccessJSONObject();
				json.put("device", JSON.parseObject(JSON.toJSON(result).toString()));
				out.write(json.toJSONString());
			} 
			else {
				out.write(JSONUtil.newErrMsgJSONObject("未找到设备").toJSONString());
			}
		}
		else{
			out.write(JSONUtil.newErrMsgJSONObject("缺少设备号参数").toJSONString());
		}
		
		out.flush();
		out.close();
	}

	public DeviceService getDeviceService() {
		return deviceService;
	}


	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}
}
